<?
include("../header.php");

/*------------------------------------------------------------------
Check for valid email address.
------------------------------------------------------------------*/
function validateEmail($email) 
{ 
	if(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3}))$', $email)) {
		return false;
	}
	else {
		return true;
	}
}
/*------------------------------------------------------------------
Function check_host()
This function will check if the entered hostname hass www. or not in front of it
------------------------------------------------------------------*/
function check_host($s_remote_host) {

	$a_host = explode(".",$s_remote_host);
	if($a_host[0] == "www") {
		$a_new_host = array($a_host[1], $a_host[2]);

        $s_remote_host = implode(".", $a_new_host);

        return $s_remote_host;
	}
    else {
		return $s_remote_host;
    }
}

	/*==================================================================

		If the button add is pressed, add email address to our file

	
	==================================================================*/
	if($_POST["btn_add"]) {

		/*------------------------------------------------------------------
		Validate email address
		------------------------------------------------------------------*/
		if(!validateEmail($_POST["s_email"])) {
			die("<b><font color='#FF0000'>[Error] Dit is geen geldig email adres!</font></b><BR><a href=\"javascript:history.back(1)\">Terug</a>");
		}
		else {
			/*------------------------------------------------------------------
			Open our mailinglist file, set filepointer to the end of the file
			------------------------------------------------------------------*/
			if(!$fp = @fopen("../../mailinglist", "a+")) {
				die("<b><font color='#FF0000'>[Error] Het mailinglist bestand kan niet worden geopend. Probeert u het later nog eens.</font></b><BR><a href=\"javascript:history.back(1)\">Terug</a>");
			}

			/*------------------------------------------------------------------
			Check if the entered email address already exists in our file!
			------------------------------------------------------------------*/
			while(!@feof ($fp)) {
				$s_data = @fgets($fp, 4069);

				if(ereg_replace("\n", "", $s_data) == $_POST["s_email"]) {
					die("<b><font color='#FF0000'>[Error] Dit email adres bestaat al in ons bestand.</font></b><BR><a href=\"javascript:history.back(1)\">Terug</a>");
				}
			}
			/*------------------------------------------------------------------
			Write the email address to our file, and add a new line to it.
			------------------------------------------------------------------*/
			if(!$fr = @fwrite($fp, $_POST["s_email"]."\n", 4096)) {
				die("<b><font color='#FF0000'>Kan het email adres niet toevoegen!</font></b><BR><a href=\"javascript:history.back(1)\">Terug</a>");
			}
			else {
				die('Uw email adres is toegevoegd! U kunt zich altijd <a href="index.php">afmelden</a> van deze lijst!');
			}

		}
	}
/*==================================================================

	If the button remove is pressed!

==================================================================*/
	if($_POST["btn_remove"]) {

		/*------------------------------------------------------------------
		Validate email address
		------------------------------------------------------------------*/
		if(!validateEmail($_POST["s_email"])) {
			die("<b><font color='#FF0000'>[Error] Dit is geen geldig email adres!</font></b><BR><a href=\"javascript:history.back(1)\">Terug</a>");
		}
		else {
			/*------------------------------------------------------------------
			Open our mailinglist file, set filepointer to the end of the file
			------------------------------------------------------------------*/
			if(!$fp = @fopen("../../mailinglist", "a+")) {
				die("<b><font color='#FF0000'>[Error] Het mailinglist bestand kan niet worden geopend. Probeert u het later nog eens.</font></b><BR><a href=\"javascript:history.back(1)\">Terug</a>");
			}

			/*------------------------------------------------------------------
			Check if the entered email address already exists in our file!
			------------------------------------------------------------------*/
			//$a_newfile = array();

			while(!@feof ($fp)) {
				$s_data = @fgets($fp, 4069);

				if(ereg_replace("\n", "", $s_data) == $_POST["s_email"]) {
					$s_has_removed = true;
				}
				else {
					$a_newfile .= $s_data;
				}				
			}
			
			@fclose($fp);
			if(!$fp = @fopen("../../mailinglist", "w+")) {
				die("<b><font color='#FF0000'>[Error] Het mailinglist bestand kan niet worden geopend. Probeert u het later nog eens.</font></b><BR><a href=\"javascript:history.back(1)\">Terug</a>");
			}

			$fw = @fwrite($fp, $a_newfile, 4069);
			
			if($s_has_removed == true) {
				die("<b><font color='##00CC00'>Het email adres is van de lijst verwijderd!</font></b><BR><a href=\"javascript:history.back(1)\">Terug</a>");
			}
			else {
				die("<b><font color='#FF0000'>[Error] Het email adres is niet gevonden!</font></b><BR><a href=\"javascript:history.back(1)\">Terug</a>");
			}
		}
	}

?>
<h1>Mailinglist</h1>
Wilt u op de hoogte gehouden worden van de ontwikkelingen rond "Leer jezelf gemakkelijk PHP", meld u dan aan voor de mailinglist. Wij sturen in ieder geval een mail zo gauw het boek uit is.<br>
<p>
<table width="75%" border="1" cellpadding="2" cellspacing="0">
<form method=post action="<? echo $_SERVER["PHP_SELF"]; ?>">
  <tr bgcolor="#009900"> 
    <td colspan="2"><b>Aanmelden</b></td>
  </tr>
  <tr> 
    <td>email adres:</td>
    <td><input name="s_email" type="text" id="s_email" size="40" maxlength="100"></td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td><input name="btn_add" type="submit" id="btn_add" value="Aanmelden"></td>
  </tr>
  </form>
</table>
<p></p>
<table width="75%" border="1" cellpadding="2" cellspacing="0">
<form method=post action="<? echo $_SERVER["PHP_SELF"]; ?>">
  <tr bgcolor="#FF0000"> 
    <td colspan="2"><b>Afmelden</b></td>
  </tr>
  <tr> 
    <td>email adres:</td>
    <td><input name="s_email" type="text" id="s_email" size="40" maxlength="100"></td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td><input name="btn_remove" type="submit" id="btn_remove" value="Afmelden"></td>
  </tr>
  </form>
</table>
<?
include("../footer.php");
?>